<?php

namespace app\merchants\services;

use app\common\loader\ModelMerchant;
use app\common\loader\Services;
use think\Exception;
use yiqiniu\extend\facade\Arrays;

/**
 * 部门管理
 */
class MerchantDepartmentService
{
    /**
     * 部门分页
     * @param array $option
     * @return array
     * @throws \think\Exception
     */
    public function getPage(array $option): array
    {
        if (empty($option['order'])) {
            $option['order'] = 'sort desc';
        }
        if (empty($option['field'])) {
            $option['field'] = 'id,mer_id,title,pid,child,code,is_sys,sort';
        }
        $pageList = ModelMerchant::MerchantDepartment()->page($option);
        Services::Helper()->showListTitle($pageList['data'], ['dept' => 'pid']);
        return $pageList;
    }

    /**
     * 部门树形列表
     * @param array $option 查询设置
     * @param int $tree 1 树形  0 普通
     * @return mixed
     * @throws \think\Exception
     */
    public function getList(array $option, int $tree = 0)
    {
        if (empty($option['order'])) {
            $option['order'] = 'sort desc';
        }
        if (empty($option['field'])) {
            $option['field'] = 'id,mer_id,title,pid,child,code,is_sys,sort';
        }
//        $list = ModelMerchant::MerchantDepartment()->select($option);
        $list = ModelMerchant::MerchantDepartment()->where($option['where'])
            ->field($option['field'] ?? ['*'])->order($option['order'])
            ->select();
        if ($tree) {
            return Arrays::toTree($list, 'id', 'pid');
        }
        return $list;

    }

    /**
     * 部门详情
     * @param array $option 查询设置
     * @return mixed
     * @throws \think\Exception
     */
    public function getInfo(array $option)
    {
        if (empty($option['field'])) {
            $option['field'] = 'id,mer_id,title,pid,sort';
        }
        $info = ModelMerchant::MerchantDepartment()->find($option);
        if (empty($info)) {
            api_exception(400, '部门不存在或已删除，请刷新后重试');
        }
        return $info;
    }

    /**
     * 删除部门
     * @param int $id
     * @param string $mer_id
     * @throws Exception
     */
    public function del(int $id, string $mer_id): void
    {
        try {
            // 只能删除子部门为空 并且 没有用户的空部门
            if (ModelMerchant::Merchant()->where('dept_id', $id)->where('mer_id', $mer_id)->count('id') > 0) {
                api_exception(API_ERROR, "部门已经应用,无法删除");
            }
            ModelMerchant::MerchantDepartment()->deptDel($id, $mer_id);
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     *  添加编辑部门
     * @param array $params
     * @return int|string|void
     * @throws Exception
     */
    public function saveDept(array $params)
    {
        try {
            return ModelMerchant::MerchantDepartment()->addEdit($params);
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * @param array $where
     * @param array $update
     * @return bool|int|string
     * @throws \think\db\exception\DbException
     */
    public function editDept(array $where, array $update)
    {
        try {
            if (empty($where) || empty($update)) {
                return true;
            }
            return ModelMerchant::MerchantDepartment()->update($where, $update);
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * 可以查看部门权限的用户
     * @param $mer_id
     * @param $departmentId
     * @return array
     * @throws Exception
     */
    public function merchantDeptSubIds($mer_id, $departmentId): array
    {
        try {
            $departmentIds = ModelMerchant::MerchantDepartment()->childIds($mer_id, $departmentId);
            $where[] = ['dept_id', 'in', $departmentIds];
            $where[] = ['mer_id', '=', $mer_id];
            return ModelMerchant::Merchant()->where($where)->column(null, "id");
        } catch (Exception $e) {
            throw $e;
        }
    }
}
